package com.fanbuchi.fukua.fragment;

import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.fanbuchi.fukua.R;
import com.fanbuchi.fukua.activity.HomeActivity;
import com.fanbuchi.fukua.activity.base.BaseFragment;
import com.fanbuchi.fukua.api.ProfileAPI;
import com.fanbuchi.fukua.api.UserAPI;
import com.fanbuchi.fukua.entity.OauthEntity;
import com.fanbuchi.fukua.entity.UserEntity;
import com.fanbuchi.fukua.manager.OauthManager;
import com.fanbuchi.fukua.net.BaseCallBack;
import com.fanbuchi.fukua.net.BaseUICallBack;
import com.squareup.okhttp.Callback;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by rabbit on 2015/6/27.
 */
public class RegFragment extends BaseFragment {

    @InjectView(R.id.userName)
    EditText userName;

    @InjectView(R.id.pass)
    android.support.v7.widget.AppCompatEditText pass;

    @InjectView(R.id.nick)
    android.support.v7.widget.AppCompatEditText nick;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }


    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_reg, container, false);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }



    public void onRegister(){
        UserEntity userEntity = new UserEntity();
        userEntity.setUserName(userName.getText().toString());
        userEntity.setNickName(nick.getText().toString());
        userEntity.setPassword(pass.getText().toString());
        onRegister(userEntity);
    }

    protected void onRegister(UserEntity user){
      SparseArray<String> _result= UserAPI.getAPI().createUser(user,regCallback());
        if (_result!=null){
            display(_result.valueAt(0));
        }
    }

    public Callback regCallback(){
        Callback _callback=  new BaseUICallBack<OauthEntity>(OauthEntity.class) {
            @Override
            public void onResponseOk(OauthEntity body) {
                if (body.getCode()!=0){
                    display(body.getDesc());
                }else{
                     OauthManager.setOauthEntity(body);
                    startActivity(HomeActivity.class);
                }
            }
        };
        return _callback;
    }



}
